package com.yuanian.timingwheel.redis;

import com.yuanian.timingwheel.model.task.base.BaseTask;
import com.yuanian.timingwheel.model.timingwheel.bucket.Bucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

 /*   private static final String prefix = "htliu-job";

    @Autowired
    private RedisTemplate redisTemplate;

    public void init(TreeMap<Integer, Bucket> buckets) {
        Set<Map.Entry<Integer, Bucket>> entrys = buckets.entrySet();
        for (Map.Entry<Integer, Bucket> entry : entrys) {
            redisTemplate.opsForHash().put(prefix + ":load", entry.getKey(), entry.getValue());
        }
    }
    public void clearBucket() {
        Map map = redisTemplate.opsForHash().entries(prefix + ":load");
        Set<Map.Entry> set = map.entrySet();
        for (Map.Entry entry : set) {
            redisTemplate.opsForHash().delete(prefix + ":load", entry.getKey());
        }
    }

    public Bucket getBucket(int round) {
        Object obj = redisTemplate.opsForHash().get(prefix + ":load", round);
        System.out.println("-----------" + obj);
        if (obj == null) {
            return null;
        }
        return (Bucket) obj;
    }

    public Bucket putBucket(int round, Bucket bucket) {
        System.out.println("putBucket:" + round + ":" + bucket);
        redisTemplate.opsForHash().put(prefix + ":load", round, bucket);
        Object obj = redisTemplate.opsForHash().get(prefix + ":load", round);
        System.out.println("-----------" + obj);
        return bucket;
    }

    @Override
    public void loadTask(Integer slot, BaseTask baseTask) {
        //加载任务要加上时间
        //redisTemplate.opsForHash().put("htliu-job:20221026", "1111", bucketQueue);
    }

    @Override
    public void waitingTask(Long taskId, BaseTask baseTask) {

    }

    @Override
    public void runningTask(Long taskId, BaseTask baseTask) {

    }

    @Override
    public void handledTask(Long taskId, BaseTask baseTask) {

    }*/
}
